package com.hejia.bulbul.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hejia.alauda.entity.PushMessage;
import com.hejia.alauda.service.MessageService;
import com.hejia.alauda.service.PushMessageService;
import com.hejia.alauda.service.UserService;
import com.hejia.alauda.utils.Pager;
import com.umeng.config.UmengConfig;

/**
 *
 * Created by clt on 14-10-21.
 */
@Controller
@RequestMapping("pushMessage")
public class PushMessageController extends BaseController {

	@Resource
	private UmengConfig umengConfig;
	
	@Resource
	private MessageService messageService;

	@Resource
	private PushMessageService pushMessageService;

	@Resource
	private UserService userService;

	/**
	 * ios推送历史消息
	 *
	 * @param pager
	 *            分页
	 * @param model
	 *            系统机型
	 * @return 页面
	 */
	@RequestMapping(value = "/ios", method = RequestMethod.GET)
	public String iosMessageList(Pager<PushMessage> pager, Model model) {
		pager = messageService.listMessagesOfModels(pager, "ios");
		model.addAttribute("page", pager);
		return "/push/ios";
	}

	/**
	 * android推送历史消息
	 *
	 * @param pager
	 *            分页
	 * @param model
	 *            系统机型
	 * @return 页面
	 */
	@RequestMapping(value = "/android", method = RequestMethod.GET)
	public String androidMessageList(Pager<PushMessage> pager, Model model) {
		pager = messageService.listMessagesOfModels(pager, "android");
		model.addAttribute("page", pager);
		return "/push/andr";
	}

	/**
	 * 消息推送
	 *
	 * @param pushMessage
	 *            推送消息
	 * @return 发送结果，成功及成功状态，失败及失败状态
	 */
	@RequestMapping(value = "/push", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> pushMessage(PushMessage pushMessage) {
		Map<String, Object> map = new HashMap<>();
		pushMessage.setCreateDate(new Date());

		try {
			if (pushMessage.getPushTime() != null) {
				messageService.savePushMessage(pushMessage);
				map.put("result", "success");
				map.put("pushMessage", pushMessage);
			} else {
				boolean send = pushMessageService.pushMessage(umengConfig, pushMessage);
				if (send) {
					pushMessage.setPushTime(new Date());
					pushMessage.setSubmitTime(new Date());
					messageService.savePushMessage(pushMessage);
					map.put("result", "success");
					map.put("pushMessage", pushMessage);
				} else {
					map.put("result", "fail");
				}
			}
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
	public String deleteAndroidMessage(@PathVariable Long id, ModelMap model,
			RedirectAttributes redirectAttributes) {
		PushMessage pMessage = pushMessageService.getPushMessageById(id);

		if (pushMessageService.deletePushMessage(id)) {
			redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
			redirectAttributes.addFlashAttribute("msg", "消息删除成功");
		} else {
			redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
			redirectAttributes.addFlashAttribute("msg", "消息删除失败");
		}
		String path = "redirect:/pushMessage/android";
		if ("ios".equals(pMessage.getModels())) {
			path = "redirect:/pushMessage/ios";
		}
		return path;
	}

}
